package com.csnf.servlet;

import com.csnf.dao.IClazzDao;
import com.csnf.dao.IStudentDao;
import com.csnf.dao.impl.ClazzDaoImpl;
import com.csnf.dao.impl.StudentDaoImpl;
import com.csnf.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;

@WebServlet({"/Studentservlet","/deleteById","/getStudentById","/addStudent"})
public class Studentservlet extends HttpServlet {
    IStudentDao studentDao = new StudentDaoImpl();
    IClazzDao clazzDao = new ClazzDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String sName = req.getParameter("sName");
//        //判断是否为空,如果是空就查询所有，否则根据名字查询
//        if (sName != null) {
//            req.setAttribute("stuList", studentDao.findByName(sName));
//        } else {
//            req.setAttribute("stuList", studentDao.findAll());
//        }
//
//    }
//}
        //获取servlet路径
        String servletPath = req.getServletPath();
        switch (servletPath) {
            //根据id查询学生信息
            case "/getStudentById":
                req.setAttribute("stu", studentDao.findById(Integer.parseInt(req.getParameter("sId"))));
                req.setAttribute("clazzList", clazzDao.findAll());
                req.getRequestDispatcher("editStudent.jsp").forward(req, resp);
                break;
            //根据id删除学生信息
            case "/deleteById":
                studentDao.deleteById(Integer.parseInt(req.getParameter("sId")));
                req.setAttribute("stuList", studentDao.findAll());
                req.getRequestDispatcher("StudentList.jsp").forward(req, resp);
                break;
            //查询学生信息
            case "/Studentservlet":
                String sName = req.getParameter("sName");
                //判断是否为空,如果是空就查询所有，否则根据名字查询
                if (sName != null) {
                    req.setAttribute("stuList", studentDao.findByName(sName));
                } else {
                    req.setAttribute("stuList", studentDao.findAll());
                }
                req.getRequestDispatcher("StudentList.jsp").forward(req, resp);
                break;
        }
        req.getRequestDispatcher("StudentList.jsp").forward(req, resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        // 获取表单数据
        String sName = req.getParameter("sName");
        String birth = req.getParameter("birth");
        String gender = req.getParameter("gender");
        String clazzId = req.getParameter("clazzId");
        //获取servlet路径判断调用
        String servletPath = req.getServletPath();
        switch (servletPath) {
            //添加学生信息
            case "/addStudent":
                studentDao.insert(new Student(null, sName, LocalDate.parse(birth), gender, Integer.parseInt(clazzId), null));
                break;
            //修改学生信息
            case "/Studentservlet":
                String sId = req.getParameter("sId");
                Student student = new Student(Integer.parseInt(sId), sName, LocalDate.parse(birth), gender, Integer.parseInt(clazzId), null);
                studentDao.update(student);
                break;
        }
            //重新查询
        req.setAttribute("stuList", studentDao.findAll());

            // 转发到学生列表页面
        req.getRequestDispatcher("StudentList.jsp").forward(req, resp);
        }

    }
